package com.ebelter.myehc.ui.fragments.bracelet;


import android.widget.TextView;

import com.ebelter.btcomlib.bases.fragment.BaseFragment;
import com.ebelter.btcomlib.models.bluetooth.products.bracelet.bean.SleepInfo;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.PieBar;
import com.ebelter.myehc.R;
import com.ebelter.myehc.beans.BraTodayData;


/**
 * 手环 睡眠
 */

public class BRA_SHM_Fragment extends BaseFragment {
    public static final String TAG = "BRA_Fragment";


    private TextView sl_totaltime_tv;
    private TextView sl_qx_p_tv;
    private TextView sl_qx_t_tv;
    private TextView sl_qm_t_tv;
    private TextView sl_qm_p_tv;
    private TextView sl_sm_t_tv;
    private TextView sl_sm_p_tv;
    private TextView xl_todayTime1_tv;
    private TextView xl_todayTime2_tv;
    private PieBar sl_piebar;

    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_bra_shm;
    }


    @Override
    protected void initView() {
        super.initView();
        FV();
    }

    private void FV() {
        sl_totaltime_tv=mRootView.findViewById(R.id.sl_totaltime_tv);
        sl_qx_p_tv=mRootView.findViewById(R.id.sl_qx_p_tv);
        sl_qx_t_tv=mRootView.findViewById(R.id.sl_qx_t_tv);
        sl_qm_t_tv=mRootView.findViewById(R.id.sl_qm_t_tv);
        sl_qm_p_tv=mRootView.findViewById(R.id.sl_qm_p_tv);
        sl_sm_t_tv=mRootView.findViewById(R.id.sl_sm_t_tv);
        sl_sm_p_tv=mRootView.findViewById(R.id.sl_sm_p_tv);
        xl_todayTime1_tv=mRootView.findViewById(R.id.toadytime_tv);
        xl_todayTime2_tv=mRootView.findViewById(R.id.toadytime2_tv);
        sl_piebar=mRootView.findViewById(R.id.sl_piebar);

    }


    //睡眠信息的回调
    public void setSleepInfo(SleepInfo sleepInfo) {

        ViewUtils.setTextViewStr(sl_totaltime_tv,timeTransition(sleepInfo.sleepTotalTime));
        ViewUtils.setTextViewStr(sl_qx_t_tv,timeTransition(sleepInfo.sleepAwakeTime));
        ViewUtils.setTextViewStr(sl_qm_t_tv,timeTransition(sleepInfo.sleepLightTime));
        ViewUtils.setTextViewStr(sl_sm_t_tv,timeTransition(sleepInfo.sleepDeepTime));

        if (sleepInfo.sleepTotalTime!=0){
            ViewUtils.setTextViewStr(sl_qx_p_tv, NumUtils.numBaoLiuWei1((float) (sleepInfo.sleepAwakeTime*1.0/sleepInfo.sleepTotalTime))+"");
            ViewUtils.setTextViewStr(sl_qm_p_tv,NumUtils.numBaoLiuWei1((float) (sleepInfo.sleepLightTime*1.0/sleepInfo.sleepTotalTime))+"");
            ViewUtils.setTextViewStr(sl_sm_p_tv,NumUtils.numBaoLiuWei1((float) (sleepInfo.sleepDeepTime*1.0/sleepInfo.sleepTotalTime))+"");
        }

    }

    private String timeTransition(int min){
        if (min<60){
            return min+" M";
        }
        int h=min/60;
        int m=min%60;
        return h+" H "+m+" M";
    }

    public void SetBraTodayData(BraTodayData braTodayData) {

        if (braTodayData.starSleepTime==0){
            LogUtils.i(TAG,"开始睡眠时间为0 没有开始睡眠时间");
            return;
        }

        //不是按照最后app统计的结果 深度睡眠如果超过浅度睡眠2倍就可以认为是未佩戴了

//        if (braTodayData.deepSleepTotal>(braTodayData.shallowSleepTotal*2)){
//            LogUtils.i(TAG,"深度睡眠如果超过浅度睡眠2倍就可以认为是未佩戴了 braTodayData.deepSleepTotal="+braTodayData.deepSleepTotal+"--braTodayData.shallowSleepTotal="+braTodayData.shallowSleepTotal);
//            return;
//        }

        int sleepTotalTime = braTodayData.deepSleepTotal+braTodayData.shallowSleepTotal;//深眠+浅眠的总时间

        if (sleepTotalTime == 0){
            LogUtils.i(TAG,"sleepTotalTime = 0 数据异常");
            return;
        }

        ViewUtils.setTextViewStr(sl_totaltime_tv,min2Hmin(sleepTotalTime));
        ViewUtils.setTextViewStr(sl_qx_t_tv,min2Hmin(braTodayData.sleeplessnessTotal));
        ViewUtils.setTextViewStr(sl_qm_t_tv,min2Hmin(braTodayData.shallowSleepTotal));
        ViewUtils.setTextViewStr(sl_sm_t_tv,min2Hmin(braTodayData.deepSleepTotal));

        float totalTime=braTodayData.sleeplessnessTotal+sleepTotalTime;
        int qx=NumUtils.numBaoLiuWei0((float) (braTodayData.sleeplessnessTotal*100.0/totalTime*1.0));
        ViewUtils.setTextViewStr(sl_qx_p_tv,qx +"%");
        int qm=NumUtils.numBaoLiuWei0((float) (braTodayData.shallowSleepTotal*100.0/totalTime*1.0));
        ViewUtils.setTextViewStr(sl_qm_p_tv,qm+"%");
       // int sm=NumUtils.numBaoLiuWei0((float) (braTodayData.deepSleepTotal*100.0/totalTime*1.0));
        int sm=100-qx-qm;
        ViewUtils.setTextViewStr(sl_sm_p_tv,sm+"%");



        ViewUtils.setTextViewStr(xl_todayTime1_tv, TimeUtils.formatTime4(braTodayData.starSleepTime));
        ViewUtils.setTextViewStr(xl_todayTime2_tv,TimeUtils.formatTime4(braTodayData.endSleepTime));
        if (sl_piebar!=null)sl_piebar.setDatas(braTodayData.list3);


    }

    public static String min2Hmin(int min){
        int h = min/60;
        int m = min%60;

        String r = "";
        if (h>0){
            r=h+"h:";
        }
        r=r+m+"min";

//        if ("0min".equals(r)){
//            return "--";
//        }
        return r;
    }

}
